﻿using Devil;
using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{
    public static class IDUtility
    {
        public static bool HexFormat { get; private set; }

        [InitializeOnLoadMethod]
        static void OnInit()
        {
            HexFormat = EditorPrefs.GetBool("HEX", false);
        }

        public static void Copy(object txt)
        {
            var src = txt == null ? null : txt.ToString();
            if (!string.IsNullOrEmpty(src))
            {
                TextEditor te = new TextEditor();
                te.text = src;
                te.OnFocus();
                te.Copy();
            }
        }

        [MenuItem("Game Toolkit/Switch HEX format")]
        static void SwitchNameIDHex()
        {
            var hex = EditorPrefs.GetBool("HEX", true);
            EditorPrefs.SetBool("HEX", !hex);
            HexFormat = hex;
        }

        public static void ShowIDConextMenu(string txt, bool hexFormat, bool editable = true)
        {
            var menu = new GenericMenu();
            GenericMenu.MenuFunction2 setHex = (v) =>
            {
                var hex = (v is bool) && (bool)v;
                if (hex != HexFormat)
                {
                    HexFormat = hex;
                    EditorPrefs.SetBool("HEX", HexFormat);
                }
            };
            menu.AddDisabledItem(new GUIContent(txt));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("复制(Copy)"), false, Copy, txt);
            menu.AddSeparator("");
            if (editable)
            {
                menu.AddItem(new GUIContent("十进制"), !hexFormat, setHex, false);
                menu.AddItem(new GUIContent("十六进制"), hexFormat, setHex, true);
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("十进制"), !hexFormat);
                menu.AddDisabledItem(new GUIContent("十六进制"), hexFormat);
            }
            menu.ShowAsContext();
        }

    }

    [CustomPropertyDrawer(typeof(NameID))]
	public class NameIDDrawer : PropertyDrawer
    {
        
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var w = Mathf.Min(position.width * 0.4f, 100);
            position.width -= w;
            EditorGUI.BeginProperty(position, label, property);
            var lv = EditorGUI.indentLevel;
            var pos = position;
            var vpos = EditorGUI.PrefixLabel(position, label);
            pos.width -= vpos.width;
            var idpos = new Rect(vpos.xMax, vpos.y, w, vpos.height);
            
            EditorGUI.indentLevel = 0;

            property.Next(true);
            var txt = EditorGUI.TextField(vpos, property.stringValue);
            var change = txt != property.stringValue;
            property.stringValue = txt;
            if (property.Next(false) && property.propertyType == SerializedPropertyType.Integer)
            {
                var id = txt.IgnoreCaseToHash();
                txt = IDUtility.HexFormat ? id.ToString("x") : id.ToString();
                //DevilEditorUtility.Hint(new Rect(vpos.x + w, vpos.y, vpos.width - w, vpos.height), txt);
                EditorGUI.LabelField(idpos, txt, DevilEditorUtility.HintStyle("label"));
                //EditorGUI.SelectableLabel(new Rect(vpos.x + w, vpos.y, vpos.width - w, vpos.height), id.ToString(), DevilEditorUtility.HintStyle("label"));
                change |= property.intValue != id;
                property.intValue = id;
            }
            GUI.changed |= change;
            EditorGUI.indentLevel = lv;
            EditorGUI.EndProperty();

            if (idpos.Contains(Event.current.mousePosition) && Event.current.type == EventType.ContextClick)
            {
                Event.current.Use();
                IDUtility.ShowIDConextMenu(txt, IDUtility.HexFormat);
            }
            else
            {
                DevilEditorUtility.DisplayArrayElementContextMenu(pos, property);
            }
        }

    }
}